<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>依赖注入 - {{a}}</h3>
      <child></child>
    </div>

    <script src="../Vue.js"></script>
    <script>
      const Child2 = {
        inject: ["count"],
        template: `
          <div>
            child2 - {{ count }}
          </div>
        `,
      };

      const Child = {
        // 注入
        // 语法和props的数组语法相同
        inject: ["count"],
        template: `
          <div>
            child  - {{ count }}
            <child2></child2>
          </div>
        `,
        components: {
          Child2,
        },
      };

      new Vue({
        el: "#app",
        data: {
          a: 40,
        },
        components: {
          Child,
        },
        // 依赖，提供者
        // 语法和data相同
        provide() {
          return {
            count: this.a,
          };
        },
        mounted() {
          setTimeout(() => {
            this.a = 50;
          }, 3000);
        },
      });
    </script>
  </body>
</html>
